package com.neusoft.bizcore.activiti.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.activiti.dto.RecordDTO;
import com.neusoft.bizcore.activiti.dto.WorkorderDTO;
import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.activiti.service.WorkOrderService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "工单管理")
@RestController
@RequestMapping(value = "/api/w/act/workorders")
public class ActWorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private ProcessInstanceService processInstanceService;

    @ApiOperation("分页查询我的待办工单信息")
    @GetMapping("/todo/p")
    public PageResultDTO<WorkorderDTO> todoWorkSearch(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        return PageResultDTO.success(this.workOrderService.todoWorkSearch(pageable, searchable, user));
    }

    @ApiOperation("我的待办工单合计")
    @GetMapping("/todo/count")
    public ResultDTO<Long> countTodoWorkSearch(final Searchable searchable,
            final UserBean user) {
        return ResultDTO.success(this.workOrderService.countTodoWorkSearch(searchable, user));
    }

    @ApiOperation("分页查询我的已办工单信息")
    @GetMapping("/down/p")
    public PageResultDTO<WorkorderDTO> doneWorkSearch(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        return PageResultDTO.success(this.workOrderService.doneWorkSearch(pageable, searchable, user));
    }

    @ApiOperation("我的已办工单合计")
    @GetMapping("/done/count")
    public ResultDTO<Long> countDoneWorkSearch(final Searchable searchable,
            final UserBean user) {
        return ResultDTO.success(this.workOrderService.countDoneWorkSearch(searchable, user));
    }

    @ApiOperation("分页查询我的工单信息")
    @GetMapping("/me/p")
    public PageResultDTO<WorkorderDTO> myWorkSearch(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        return PageResultDTO.success(this.workOrderService.myWorkSearch(pageable, searchable, user));
    }

    @ApiOperation("我的工单合计")
    @GetMapping("/me/count")
    public ResultDTO<Long> countMeWorkSearch(final Searchable searchable,
            final UserBean user) {
        return ResultDTO.success(this.workOrderService.countMeWorkSearch(searchable, user));
    }

    @ApiOperation("撤销工单")
    @PostMapping("/{workNum}/revoke")
    public ResultDTO<Void> revoke(@PathVariable final String workNum, final UserBean user) {
        this.workOrderService.revoke(workNum, user);
        return ResultDTO.success();
    }

    @ApiOperation("新建暂存状态的工单删除")
    @DeleteMapping("/{workNum}")
    public ResultDTO<Void> delete(@PathVariable final String workNum) {
        this.workOrderService.delete(workNum);
        return ResultDTO.success();
    }

    @ApiOperation("获取工单履历")
    @GetMapping("/records/{workNum}")
    public ResultListDTO<RecordDTO> records(@PathVariable final String workNum, final UserBean user) {
        final List<RecordDTO> result = this.processInstanceService.getRecords(workNum, user);
        return ResultListDTO.success(result);
    }

    @ApiOperation("获取工单履历")
    @GetMapping("/records/nouser/{workNum}")
    public ResultListDTO<RecordDTO> records(@PathVariable final String workNum) {
        final List<RecordDTO> result = this.processInstanceService.getRecords(workNum);
        return ResultListDTO.success(result);
    }

    @ApiOperation("工单流程图")
    @GetMapping("/{type}/image/original")
    public void originalImage(final HttpServletResponse response, @PathVariable final String type) {

        OutputStream out;
        try {
            out = response.getOutputStream();
            final InputStream is =
                    this.processInstanceService.getImage(type, null, false);
            if (null != is) {
                final BufferedImage image = ImageIO.read(is);
                response.setContentType("image/png");
                ImageIO.write(image, "png", out);
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("工单流程运行图")
    @GetMapping("/{workNum}/image/run")
    public void runImage(final HttpServletResponse response, @PathVariable final String workNum) {

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            final InputStream is =
                    this.processInstanceService.getImage(null, workNum, true);
            if (null != is) {
                final BufferedImage image = ImageIO.read(is);
                response.setContentType("image/png");
                ImageIO.write(image, "png", out);
            }
        } catch (final IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (final IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
